package cn.edu.sdjzu.xg2106.controller;

import cn.edu.sdjzu.xg2106.domain.University;
import cn.edu.sdjzu.xg2106.exception.BysjException;
import cn.edu.sdjzu.xg2106.service.UniversityService;
import cn.edu.sdjzu.xg2106.util.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "UniversityDeleteCtl", value = "/universityDeleteCtl")
public class UniversityDeleteCtrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject message_jsonObj = new JSONObject();
        response.setContentType("application/json;charset=UTF-8");
        String req_jsonStr = JSONUtil.getJSON(request);
        JSONObject req_jsonObj= JSON.parseObject(req_jsonStr);
        if (req_jsonObj==null){
            message_jsonObj.put("message","未收到请求信息");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try {
            University university = JSON.parseObject(req_jsonStr,University.class);
            UniversityService.getInstance().delete(university.getId());
            message_jsonObj.put("message","删除成功");
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e){
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
        }
    }

}
